/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;


import com.cashmate.hibernate.dao.BusinessDAO;
import com.cashmate.hibernate.dao.CreditinvestigationDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoanapplicationDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.dao.ReputationDAO;
import com.cashmate.hibernate.dao.SourceofincomeDAO;
import com.cashmate.hibernate.dao.TypeofcollateralDAO;
import com.cashmate.hibernate.pojo.Borrowerinfo;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Businesses;
import com.cashmate.hibernate.pojo.CollateralCreditinv;
import com.cashmate.hibernate.pojo.Creditinvestigations;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Documentspassed;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Informants;
import com.cashmate.hibernate.pojo.Loanapplications;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.hibernate.pojo.Notesreceivable;
import com.cashmate.hibernate.pojo.Othersourceofincome;
import com.cashmate.hibernate.pojo.Personalandrealproperties;
import com.cashmate.hibernate.pojo.Reputation;
import com.cashmate.hibernate.pojo.ReputationCreditinv;
import com.cashmate.hibernate.pojo.SourceCreditinv;
import com.cashmate.hibernate.pojo.Sourceofincome;
import com.cashmate.hibernate.pojo.Typeofcollateral;
import com.cashmate.hibernate.pojo.Vehicle;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Lee
 */
public class AddCreditInv extends ActionSupport implements ServletRequestAware {

    private Creditinvestigations c;
    private CreditinvestigationDAO cd = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private LoanapplicationDAO ld = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private EmployeeDAO emd = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private ReputationDAO rd = (ReputationDAO) ServletActionContext.getServletContext().getAttribute("reputationDAO");
    private TypeofcollateralDAO tcd = (TypeofcollateralDAO) ServletActionContext.getServletContext().getAttribute("typeofcollateralDAO");
    private SourceofincomeDAO sid = (SourceofincomeDAO) ServletActionContext.getServletContext().getAttribute("sourceofincomeDAO");
    private BusinessDAO bd = (BusinessDAO) ServletActionContext.getServletContext().getAttribute("businessDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private String nickname;
    private String actionstatus;
    private String addcreditstatus;
    private String addressfloorarea;
    private String addresslotarea;
    private String addressstability;
    private String addressmodel;
    private String addressmake;
    private String addressland;
    private String addressmonthlyrental;
    private String addresslandlordname;
    private String addresslandlordaddress;
    private String addresslandlordphoneno;
    private String spousedegree;
    private String spouseschool;
    private String previousstreetaddress;
    private String previousbarangay;
    private String previouscityormunicipality;
    private String previousprovince;
    private String previouspostalcode;
    private String parentsstreetaddress;
    private String parentsbarangay;
    private String parentscityormunicipality;
    private String parentsprovince;
    private String parentspostalcode;
    private String fatherinlawfirstname;
    private String fatherinlawmiddlename;
    private String fatherinlawlastname;
    private String motherinlawfirstname;
    private String motherinlawmiddlename;
    private String motherinlawlastname;
    private String fatherinlawemployer;
    private String motherinlawemployer;
    private String fatherinlawposition;
    private String motherinlawposition;
    private String parentsinlawstreetaddress;
    private String parentsinlawbarangay;
    private String parentsinlawcityormunicipality;
    private String parentsinlawprovince;
    private String parentsinlawpostalcode;
    private String presentaddressstatus;
    private String presentaddresslengthofstayinyears;
    private String presentaddresslengthofstayinmonths;
    private String stocksworthamount;
    private String stocksworth;
    private String remarks;
    private String creditconsultantrecommendation;
    private String branchmanagerrecommendation;
    private String areamanagerrecommendation;
    private String investigationdate;
    private String spouseyeargraduated;
    private String previouslengthofstayinyears;
    private String previouslengthofstayinmonths;
    private String parentslengthofstayinyear;
    private String parentslengthofstayinmonths;
    private String fatherinlawage;
    private String motherinlawage;
    private String parentsinlawlengthofstayinyears;
    private String parentsinlawlengthofstayinmonths;
    //document
    private String loanverid, employeeid, docdate, docnoci, doctypeci;
    //vehicle
    private String[] vtype, make, model;
    //documents passed
    private String[] documenttype, documentno, copytype;
    //informants
    private String[] ifirstname, imiddlename, ilastname, iaddress, irelationshiptoborrower, ilengthofrelationshipinyears;
    //personal
    private String[] description, nature, location, value;
    //savings
    private String[] bank, stotype, accountno, amount;
    //other
    private String[] type, employer, address, position, income;
    //reputation
    private String[] reputation;
    //collateral
    private String[] collateral;
    //source of income
    private String[] sourceofincome;
    //Borrowerinfo
    private String degree, school, yeargraduated, fatherfirstname, fathermiddlename, fatherlastname, motherfirstname,
            mothermiddlename, motherlastname, fatheremployer, motheremployer, fatherposition, motherposition,
            fatherage, motherage;
    //Requirement
    private String[] requirement;
    private String successurl;
    //Business
    private String[] businessid;
    private String[] businessname;
    private String[] businessfloorarea;
    private String[] businesslandarea;
    private String[] businessstability;
    private String[] businessmodel;
    private String[] businessmake;
    private String[] businessland;
    private String[] dailyaveragesales;
    private String[] businesslandlordname;
    private String[] businessmonthlyrental;
    private String[] businesslandlordaddress;
    private String[] businesslandlordphoneno;
    private String[] businesstype;
    //image
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private HttpServletRequest servletRequest;

    public void setBusinessfloorarea(String[] businessfloorarea) {
        this.businessfloorarea = businessfloorarea;
    }

    public void setBusinessid(String[] businessid) {
        this.businessid = businessid;
    }

    public void setBusinessland(String[] businessland) {
        this.businessland = businessland;
    }

    public void setBusinesslandarea(String[] businesslandarea) {
        this.businesslandarea = businesslandarea;
    }

    public void setBusinesslandlordaddress(String[] businesslandlordaddress) {
        this.businesslandlordaddress = businesslandlordaddress;
    }

    public void setBusinesslandlordname(String[] businesslandlordname) {
        this.businesslandlordname = businesslandlordname;
    }

    public void setBusinesslandlordphoneno(String[] businesslandlordphoneno) {
        this.businesslandlordphoneno = businesslandlordphoneno;
    }

    public void setBusinessmake(String[] businessmake) {
        this.businessmake = businessmake;
    }

    public void setBusinessmodel(String[] businessmodel) {
        this.businessmodel = businessmodel;
    }

    public void setBusinessmonthlyrental(String[] businessmonthlyrental) {
        this.businessmonthlyrental = businessmonthlyrental;
    }

    public void setBusinessname(String[] businessname) {
        this.businessname = businessname;
    }

    public void setBusinessstability(String[] businessstability) {
        this.businessstability = businessstability;
    }

    public void setBusinesstype(String[] businesstype) {
        this.businesstype = businesstype;
    }

    public void setDailyaveragesales(String[] dailyaveragesales) {
        this.dailyaveragesales = dailyaveragesales;
    }

    public void setSourceofincome(String[] sourceofincome) {
        this.sourceofincome = sourceofincome;
    }

    public String getSuccessurl() {
        return successurl;
    }

    public void setCollateral(String[] collateral) {
        this.collateral = collateral;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setRequirement(String[] requirement) {
        this.requirement = requirement;
    }

    public void setStocksworthamount(String stocksworthamount) {
        this.stocksworthamount = stocksworthamount;
    }

    public String getActionstatus() {
        return actionstatus;
    }

    public void setActionstatus(String actionstatus) {
        this.actionstatus = actionstatus;
    }

    public void setDocdate(String docdate) {
        this.docdate = docdate;
    }

    public void setDocnoci(String docnoci) {
        this.docnoci = docnoci;
    }

    public void setDoctypeci(String doctypeci) {
        this.doctypeci = doctypeci;
    }

    public void setEmployeeid(String employeeid) {
        this.employeeid = employeeid;
    }

    public String getLoanverid() {
        return loanverid;
    }

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public String getAddcreditstatus() {
        return addcreditstatus;
    }

    public void setAddcreditstatus(String addcreditstatus) {
        this.addcreditstatus = addcreditstatus;
    }

    public void setAddressfloorarea(String addressfloorarea) {
        this.addressfloorarea = addressfloorarea;
    }

    public void setAddressland(String addressland) {
        this.addressland = addressland;
    }

    public void setAddresslandlordaddress(String addresslandlordaddress) {
        this.addresslandlordaddress = addresslandlordaddress;
    }

    public void setAddresslandlordname(String addresslandlordname) {
        this.addresslandlordname = addresslandlordname;
    }

    public void setAddresslandlordphoneno(String addresslandlordphoneno) {
        this.addresslandlordphoneno = addresslandlordphoneno;
    }

    public void setAddresslotarea(String addresslotarea) {
        this.addresslotarea = addresslotarea;
    }

    public void setAddressmake(String addressmake) {
        this.addressmake = addressmake;
    }

    public void setAddressmodel(String addressmodel) {
        this.addressmodel = addressmodel;
    }

    public void setAddressmonthlyrental(String addressmonthlyrental) {
        this.addressmonthlyrental = addressmonthlyrental;
    }

    public void setAddressstability(String addressstability) {
        this.addressstability = addressstability;
    }

    public void setAreamanagerrecommendation(String areamanagerrecommendation) {
        this.areamanagerrecommendation = areamanagerrecommendation;
    }

    public void setBranchmanagerrecommendation(String branchmanagerrecommendation) {
        this.branchmanagerrecommendation = branchmanagerrecommendation;
    }

    public void setC(Creditinvestigations c) {
        this.c = c;
    }

    public void setCreditconsultantrecommendation(String creditconsultantrecommendation) {
        this.creditconsultantrecommendation = creditconsultantrecommendation;
    }

    public void setFatherinlawage(String fatherinlawage) {
        this.fatherinlawage = fatherinlawage;
    }

    public void setFatherinlawemployer(String fatherinlawemployer) {
        this.fatherinlawemployer = fatherinlawemployer;
    }

    public void setFatherinlawfirstname(String fatherinlawfirstname) {
        this.fatherinlawfirstname = fatherinlawfirstname;
    }

    public void setFatherinlawlastname(String fatherinlawlastname) {
        this.fatherinlawlastname = fatherinlawlastname;
    }

    public void setFatherinlawmiddlename(String fatherinlawmiddlename) {
        this.fatherinlawmiddlename = fatherinlawmiddlename;
    }

    public void setFatherinlawposition(String fatherinlawposition) {
        this.fatherinlawposition = fatherinlawposition;
    }

    public void setInvestigationdate(String investigationdate) {
        this.investigationdate = investigationdate;
    }

    public void setMotherinlawage(String motherinlawage) {
        this.motherinlawage = motherinlawage;
    }

    public void setMotherinlawemployer(String motherinlawemployer) {
        this.motherinlawemployer = motherinlawemployer;
    }

    public void setMotherinlawfirstname(String motherinlawfirstname) {
        this.motherinlawfirstname = motherinlawfirstname;
    }

    public void setMotherinlawlastname(String motherinlawlastname) {
        this.motherinlawlastname = motherinlawlastname;
    }

    public void setMotherinlawmiddlename(String motherinlawmiddlename) {
        this.motherinlawmiddlename = motherinlawmiddlename;
    }

    public void setMotherinlawposition(String motherinlawposition) {
        this.motherinlawposition = motherinlawposition;
    }

    public void setParentsbarangay(String parentsbarangay) {
        this.parentsbarangay = parentsbarangay;
    }

    public void setParentscityormunicipality(String parentscityormunicipality) {
        this.parentscityormunicipality = parentscityormunicipality;
    }

    public void setParentsinlawbarangay(String parentsinlawbarangay) {
        this.parentsinlawbarangay = parentsinlawbarangay;
    }

    public void setParentsinlawcityormunicipality(String parentsinlawcityormunicipality) {
        this.parentsinlawcityormunicipality = parentsinlawcityormunicipality;
    }

    public void setParentsinlawlengthofstayinmonths(String parentsinlawlengthofstayinmonths) {
        this.parentsinlawlengthofstayinmonths = parentsinlawlengthofstayinmonths;
    }

    public void setParentsinlawlengthofstayinyears(String parentsinlawlengthofstayinyears) {
        this.parentsinlawlengthofstayinyears = parentsinlawlengthofstayinyears;
    }

    public void setParentsinlawpostalcode(String parentsinlawpostalcode) {
        this.parentsinlawpostalcode = parentsinlawpostalcode;
    }

    public void setParentsinlawprovince(String parentsinlawprovince) {
        this.parentsinlawprovince = parentsinlawprovince;
    }

    public void setParentsinlawstreetaddress(String parentsinlawstreetaddress) {
        this.parentsinlawstreetaddress = parentsinlawstreetaddress;
    }

    public void setParentslengthofstayinmonths(String parentslengthofstayinmonths) {
        this.parentslengthofstayinmonths = parentslengthofstayinmonths;
    }

    public void setParentslengthofstayinyear(String parentslengthofstayinyear) {
        this.parentslengthofstayinyear = parentslengthofstayinyear;
    }

    public void setParentspostalcode(String parentspostalcode) {
        this.parentspostalcode = parentspostalcode;
    }

    public void setParentsprovince(String parentsprovince) {
        this.parentsprovince = parentsprovince;
    }

    public void setParentsstreetaddress(String parentsstreetaddress) {
        this.parentsstreetaddress = parentsstreetaddress;
    }

    public void setPresentaddressstatus(String presentaddressstatus) {
        this.presentaddressstatus = presentaddressstatus;
    }

    public void setPreviousbarangay(String previousbarangay) {
        this.previousbarangay = previousbarangay;
    }

    public void setPreviouscityormunicipality(String previouscityormunicipality) {
        this.previouscityormunicipality = previouscityormunicipality;
    }

    public void setPreviouslengthofstayinmonths(String previouslengthofstayinmonths) {
        this.previouslengthofstayinmonths = previouslengthofstayinmonths;
    }

    public void setPreviouslengthofstayinyears(String previouslengthofstayinyears) {
        this.previouslengthofstayinyears = previouslengthofstayinyears;
    }

    public void setPreviouspostalcode(String previouspostalcode) {
        this.previouspostalcode = previouspostalcode;
    }

    public void setPreviousprovince(String previousprovince) {
        this.previousprovince = previousprovince;
    }

    public void setPreviousstreetaddress(String previousstreetaddress) {
        this.previousstreetaddress = previousstreetaddress;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public void setSpousedegree(String spousedegree) {
        this.spousedegree = spousedegree;
    }

    public void setSpouseschool(String spouseschool) {
        this.spouseschool = spouseschool;
    }

    public void setSpouseyeargraduated(String spouseyeargraduated) {
        this.spouseyeargraduated = spouseyeargraduated;
    }

    public void setStocksworth(String stocksworth) {
        this.stocksworth = stocksworth;
    }

    public void setReputation(String[] reputation) {
        this.reputation = reputation;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public void setFatherage(String fatherage) {
        this.fatherage = fatherage;
    }

    public void setFatheremployer(String fatheremployer) {
        this.fatheremployer = fatheremployer;
    }

    public void setFatherfirstname(String fatherfirstname) {
        this.fatherfirstname = fatherfirstname;
    }

    public void setFatherlastname(String fatherlastname) {
        this.fatherlastname = fatherlastname;
    }

    public void setFathermiddlename(String fathermiddlename) {
        this.fathermiddlename = fathermiddlename;
    }

    public void setFatherposition(String fatherposition) {
        this.fatherposition = fatherposition;
    }

    public void setMotherage(String motherage) {
        this.motherage = motherage;
    }

    public void setMotheremployer(String motheremployer) {
        this.motheremployer = motheremployer;
    }

    public void setMotherfirstname(String motherfirstname) {
        this.motherfirstname = motherfirstname;
    }

    public void setMotherlastname(String motherlastname) {
        this.motherlastname = motherlastname;
    }

    public void setMothermiddlename(String mothermiddlename) {
        this.mothermiddlename = mothermiddlename;
    }

    public void setMotherposition(String motherposition) {
        this.motherposition = motherposition;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public void setYeargraduated(String yeargraduated) {
        this.yeargraduated = yeargraduated;
    }

    public void setAccountno(String[] accountno) {
        this.accountno = accountno;
    }

    public void setAmount(String[] amount) {
        this.amount = amount;
    }

    public void setBank(String[] bank) {
        this.bank = bank;
    }

    public void setDescription(String[] description) {
        this.description = description;
    }

    public void setEmployer(String[] employer) {
        this.employer = employer;
    }

    public void setIaddress(String[] iaddress) {
        this.iaddress = iaddress;
    }

    public void setIfirstname(String[] ifirstname) {
        this.ifirstname = ifirstname;
    }

    public void setIlastname(String[] ilastname) {
        this.ilastname = ilastname;
    }

    public void setIlengthofrelationshipinyears(String[] ilengthofrelationshipinyears) {
        this.ilengthofrelationshipinyears = ilengthofrelationshipinyears;
    }

    public void setImiddlename(String[] imiddlename) {
        this.imiddlename = imiddlename;
    }

    public void setIrelationshiptoborrower(String[] irelationshiptoborrower) {
        this.irelationshiptoborrower = irelationshiptoborrower;
    }

    public void setLocation(String[] location) {
        this.location = location;
    }

    public void setMake(String[] makevehicle) {
        this.make = makevehicle;
    }

    public void setModel(String[] modelvehicle) {
        this.model = modelvehicle;
    }

    public void setNature(String[] nature) {
        this.nature = nature;
    }

    public void setAddress(String[] otheraddress) {
        this.address = otheraddress;
    }

    public void setIncome(String[] otherincome) {
        this.income = otherincome;
    }

    public void setPosition(String[] otherposition) {
        this.position = otherposition;
    }

    public void setType(String[] othersrctype) {
        this.type = othersrctype;
    }

    public void setCopytype(String[] pcopytype) {
        this.copytype = pcopytype;
    }

    public void setDocumentno(String[] pdocumentno) {
        this.documentno = pdocumentno;
    }

    public void setDocumenttype(String[] pdocumenttype) {
        this.documenttype = pdocumenttype;
    }

    public void setVtype(String[] type) {
        this.vtype = type;
    }

    public void setStotype(String[] type) {
        this.stotype = type;
    }

    public void setValue(String[] value) {
        this.value = value;
    }

    public void setPresentaddresslengthofstayinmonths(String presentaddresslengthofstayinmonths) {
        this.presentaddresslengthofstayinmonths = presentaddresslengthofstayinmonths;
    }

    public void setPresentaddresslengthofstayinyears(String presentaddresslengthofstayinyears) {
        this.presentaddresslengthofstayinyears = presentaddresslengthofstayinyears;
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

    @Action(value = "/add-credit", results = {
        @Result(name = "input", location = "${successurl}"),
        @Result(name = "success", location = "${successurl}")
    })
    @Override
    public String execute() {
        successurl = "../../loanapplication.jsp?cat=credit";
        Borrowerinfo borinfo = null;
        Employees e = null;
        Borrowers b = null;
        Documents d = null;
        Loanverification lv = null;
        Loanapplications l = null;
        Creditinvestigations ci = null;
        List<Documentspassed> listdoc = new ArrayList<Documentspassed>();
        List<Informants> listinf = new ArrayList<Informants>();
        List<Personalandrealproperties> listper = new ArrayList<Personalandrealproperties>();
        List<Othersourceofincome> listoth = new ArrayList<Othersourceofincome>();
        List<Vehicle> listveh = new ArrayList<Vehicle>();
        List<Notesreceivable> listnotes = new ArrayList<Notesreceivable>();
        List<ReputationCreditinv> listrci = new ArrayList<ReputationCreditinv>();
        List<CollateralCreditinv> listcci = new ArrayList<CollateralCreditinv>();
        List<SourceCreditinv> listsci = new ArrayList<SourceCreditinv>();
        List<Businesses> listbusadd = new ArrayList<Businesses>();
        List<Businesses> listbusupd = new ArrayList<Businesses>();
        List<Integer> listbusid = new ArrayList<Integer>();

        try {
            Integer.parseInt(employeeid);
            Integer.parseInt(loanverid);



            if (!fatherage.equals("")) {
                Integer.parseInt(fatherage);
            }

            if (!motherage.equals("")) {
                Integer.parseInt(motherage);
            }

            if (!stocksworthamount.equals("")) {
                Integer.parseInt(stocksworthamount);
            }
            if (!spouseyeargraduated.equals("")) {
                Integer.parseInt(spouseyeargraduated);
            }
            if (!previouslengthofstayinyears.equals("")) {
                Integer.parseInt(previouslengthofstayinyears);
            }
            if (!previouslengthofstayinmonths.equals("")) {
                Integer.parseInt(previouslengthofstayinmonths);
            }
            if (!presentaddresslengthofstayinyears.equals("")) {
                Integer.parseInt(presentaddresslengthofstayinyears);
            }
            if (!presentaddresslengthofstayinmonths.equals("")) {
                Integer.parseInt(presentaddresslengthofstayinyears);
            }
            if (!parentslengthofstayinyear.equals("")) {
                Integer.parseInt(parentslengthofstayinyear);
            }
            if (!parentslengthofstayinmonths.equals("")) {
                Integer.parseInt(parentslengthofstayinmonths);
            }
            if (!fatherinlawage.equals("")) {
                Integer.parseInt(fatherinlawage);
            }
            if (!motherinlawage.equals("")) {
                Integer.parseInt(motherinlawage);
            }
            if (!parentsinlawlengthofstayinyears.equals("")) {
                Integer.parseInt(parentsinlawlengthofstayinyears);
            }
            if (!parentsinlawlengthofstayinmonths.equals("")) {
                Integer.parseInt(parentsinlawlengthofstayinmonths);
            }

        } catch (Exception g) {
            g.printStackTrace();
            actionstatus = "A string value was provided for a textfield that expects a numeric value.";
            return INPUT;
        }

        try {
            d = new Documents(docnoci, doctypeci, docdate, true);
            d.setFilename(userImageFileName);
            e = emd.getByIdNumber(employeeid);
            lv = lvd.getById(Integer.parseInt(loanverid));
            l = ld.getByLoanVerId(Integer.parseInt(loanverid));


//            String addressfloorarea, String addresslotarea, String addressstability, String addressmodel, 
//            String addressmake, String addressland, String parentsstreetaddress, String parentsbarangay, 
//            String parentscityormunicipality, String parentsprovince, 
//            int presentaddresslengthofstayinyears, int presentaddresslengthofstayinmonths, 
//                    String presentaddressstatus, String investigationdate, String nickname

            ci = new Creditinvestigations(nickname, addressfloorarea, addresslotarea, addressstability, addressmodel, addressmake, addressland,
                    Integer.parseInt(presentaddresslengthofstayinyears), Integer.parseInt(presentaddresslengthofstayinmonths),
                    presentaddressstatus, docdate, "saved");
            ci.setLoanverifications(lv);
            ci.setDocuments(d);
            ci.setEmployees(e);
            ci.setParentsstreetaddress(parentsstreetaddress);
            ci.setParentsbarangay(parentsbarangay);
            ci.setParentscityormunicipality(parentscityormunicipality);
            ci.setParentsprovince(parentsprovince);
            ci.setAddressmonthlyrental(addressmonthlyrental);
            ci.setAddresslandlordname(addresslandlordname);
            ci.setAddresslandlordaddress(addresslandlordaddress);
            ci.setAddresslandlordphoneno(addresslandlordphoneno);
            ci.setSpousedegree(spousedegree);
            ci.setSpouseschool(spouseschool);
            ci.setPreviousstreetaddress(previousstreetaddress);
            ci.setPreviousbarangay(previousbarangay);
            ci.setPreviouscityormunicipality(previouscityormunicipality);
            ci.setPreviousprovince(previousprovince);
            ci.setPreviouspostalcode(previouspostalcode);
            ci.setParentspostalcode(parentspostalcode);
            ci.setFatherinlawfirstname(fatherinlawfirstname);
            ci.setFatherinlawmiddlename(fatherinlawmiddlename);
            ci.setFatherinlawlastname(fatherinlawlastname);
            ci.setMotherinlawfirstname(motherinlawfirstname);
            ci.setMotherinlawmiddlename(motherinlawmiddlename);
            ci.setMotherinlawlastname(motherinlawlastname);
            ci.setMotherinlawemployer(motherinlawemployer);
            ci.setFatherinlawemployer(fatherinlawemployer);
            ci.setFatherinlawposition(fatherinlawposition);
            ci.setMotherinlawposition(motherinlawposition);
            ci.setParentsinlawstreetaddress(parentsinlawstreetaddress);
            ci.setParentsinlawbarangay(parentsinlawbarangay);
            ci.setParentsinlawcityormunicipality(parentsinlawcityormunicipality);
            ci.setParentsinlawprovince(parentsinlawprovince);
            ci.setParentsinlawpostalcode(parentsinlawpostalcode);
            ci.setStocksworthamount(Integer.parseInt(stocksworthamount));
            ci.setStocksworthdescription(stocksworth);
            ci.setRemarks(remarks);
            ci.setCreditconsultantrecommendation(creditconsultantrecommendation);
            ci.setBranchmanagerrecommendation(branchmanagerrecommendation);
            ci.setAreamanagerrecommendation(areamanagerrecommendation);
            ci.setSpouseyeargraduated(Integer.parseInt(spouseyeargraduated));
            ci.setPreviouslengthofstayinmonths(Integer.parseInt(parentslengthofstayinmonths));
            ci.setPreviouslengthofstayinyears(Integer.parseInt(previouslengthofstayinyears));
            ci.setParentslengthofstayinmonths(Integer.parseInt(parentslengthofstayinmonths));
            ci.setParentslengthofstayinyear(Integer.parseInt(parentslengthofstayinyear));
            ci.setFatherinlawage(Integer.parseInt(fatherinlawage));
            ci.setMotherinlawage(Integer.parseInt(motherinlawage));
            ci.setParentsinlawlengthofstayinmonths(Integer.parseInt(parentsinlawlengthofstayinmonths));
            ci.setParentsinlawlengthofstayinyears(Integer.parseInt(parentsinlawlengthofstayinmonths));

            b = l.getBorrowers();
            borinfo = new Borrowerinfo();
            borinfo.setDegree(degree);
            borinfo.setSchool(school);
            borinfo.setYeargraduated(yeargraduated);
            borinfo.setFatheremployer(fatheremployer);
            borinfo.setFatherfirstname(fatherfirstname);
            borinfo.setFatherlastname(fatherlastname);
            borinfo.setFathermiddlename(fathermiddlename);
            borinfo.setFatherposition(fatherposition);
            borinfo.setMotheremployer(motheremployer);
            borinfo.setMotherfirstname(motherfirstname);
            borinfo.setMotherlastname(motherlastname);
            borinfo.setMothermiddlename(mothermiddlename);
            borinfo.setMotherposition(motherposition);
            borinfo.setFatherage(Integer.parseInt(fatherage));
            borinfo.setMotherage(Integer.parseInt(motherage));
            borinfo.setBorrowers(b);

            if (businessname.length != 0 && businesstype.length != 0) {
                for (int i = 0; i < businessname.length; i++) {
                    if (!businessid[i].equals("")) {
                        //update
                        try {
                            Businesses bus = bd.get(Integer.parseInt(businessid[i]));
                            bus.setBusinessname(businessname[i]);
                            bus.setBusinesstype(businesstype[i]);
                            bus.setFloorarea(businessfloorarea[i]);
                            bus.setLotarea(businesslandarea[i]);
                            bus.setStability(businessstability[i]);
                            bus.setModel(businessmodel[i]);
                            bus.setMake(businessmake[i]);
                            bus.setLand(businessland[i]);
                            bus.setDailyaveragesales(Integer.parseInt(dailyaveragesales[i]));
                            bus.setLandlordaddress(businesslandlordaddress[i]);
                            bus.setLandlordname(businesslandlordname[i]);
                            bus.setLandlordphoneno(businesslandlordphoneno[i]);
                            if (businessmonthlyrental[i].equals("")) {
                                bus.setMonthlyrental(Integer.parseInt(businessmonthlyrental[i]));
                            }


                            listbusupd.add(bus);
                            listbusid.add(Integer.parseInt(businessid[i]));
                        } catch (Exception h) {
                            h.printStackTrace();
                        }

                    } else {
//                        System.out.println("listbusinessadd");
                        Businesses bus = new Businesses();
                        bus.setBarangay("empty");
                        bus.setStreetaddress("empty");
                        bus.setCityormunicipality("empty");
                        bus.setProvince("empty");
                        bus.setPostalcode("empty");
                        bus.setBusinessname(businessname[i]);
                        bus.setBusinesstype(businesstype[i]);
                        bus.setFloorarea(businessfloorarea[i]);
                        bus.setLotarea(businesslandarea[i]);
                        bus.setStability(businessstability[i]);
                        bus.setModel(businessmodel[i]);
                        bus.setMake(businessmake[i]);
                        bus.setLand(businessland[i]);
                        bus.setDailyaveragesales(Integer.parseInt(dailyaveragesales[i]));
                        bus.setLandlordaddress(businesslandlordaddress[i]);
                        bus.setLandlordname(businesslandlordname[i]);
                        bus.setLandlordphoneno(businesslandlordphoneno[i]);
                        bus.setMonthlyrental(Integer.parseInt(businessmonthlyrental[i]));
                        bus.setLoanapplications(l);
//                        System.out.println(bus);
                        listbusadd.add(bus);
                    }
                }
            }


            if (documenttype.length != 0 && documentno.length != 0 && copytype.length != 0) {
                for (int i = 0; i < documenttype.length; i++) {
                    if (!documenttype[i].equals("") && !documentno[i].equals("") && !copytype[i].equals("")) {
//                        System.out.println("(" + i + ") " + documenttype[i] + " " + documentno[i] + " " + copytype[i]);
                        Documentspassed dp = new Documentspassed(ci, documenttype[i], copytype[i], documentno[i]);
                        listdoc.add(dp);
                    }
                }
            }
//            private String[] ifirstname, imiddlename, ilastname, iaddress, irelationshiptoborrower, ilengthofrelationshipinyears;
            if (ifirstname.length != 0 && imiddlename.length != 0 && ilastname.length != 0 && iaddress.length != 0
                    && irelationshiptoborrower.length != 0 && ilengthofrelationshipinyears.length != 0) {
                for (int i = 0; i < ifirstname.length; i++) {
                    if (!ifirstname[i].equals("") && !imiddlename[i].equals("") && !ilastname[i].equals("") && !iaddress[i].equals("")
                            && !irelationshiptoborrower[i].equals("") && !ilengthofrelationshipinyears[i].equals("")) {
//                        System.out.println("INFORMANT(" + i + ") " + ifirstname[i] + " " + imiddlename[i] + " " + ilastname[i]);
                        try {
                            int length = Integer.parseInt(ilengthofrelationshipinyears[i]);
                            Informants informant = new Informants(ci, ifirstname[i], imiddlename[i], ilastname[i], iaddress[i],
                                    irelationshiptoborrower[i], length);
                            listinf.add(informant);
                        } catch (Exception k) {
                            actionstatus = "Values specified for informant's length of relationship is not valid.";
                            return ERROR;
                        }

                    }
                }
            }
            if (nature.length != 0 && value.length != 0 && location.length != 0 && description.length != 0) {
                for (int i = 0; i < nature.length; i++) {
                    if (!nature[i].equals("") && !value[i].equals("") && !location[i].equals("") && !description[i].equals("")) {
//                        System.out.println("(" + i + ") " + nature[i] + " " + value[i] + " " + location[i]);
                        try {
                            int val = Integer.parseInt(value[i]);
                            Personalandrealproperties parp = new Personalandrealproperties(ci, description[i], nature[i], location[i], val);
                            listper.add(parp);
                        } catch (Exception m) {
                            actionstatus = "Values specified for property's value is not valid.";
                            return ERROR;
                        }

                    }
                }
            }
            if (type.length != 0 && employer.length != 0 && address.length != 0 && position.length != 0 && income.length != 0) {
                for (int i = 0; i < type.length; i++) {
                    if (!type[i].equals("") && !employer[i].equals("") && !address[i].equals("") && !position[i].equals("") && !income[i].equals("")) {
//                        System.out.println("(" + i + ") " + type[i] + " " + employer[i] + " " + address[i]);
                        try {
                            int inc = Integer.parseInt(income[i]);
                            Othersourceofincome os = new Othersourceofincome(ci, type[i], employer[i], address[i], position[i], inc);
                            listoth.add(os);
                        } catch (Exception m) {
                            actionstatus = "Values specified for other source of income's income value is not valid.";
                            return ERROR;
                        }

                    }
                }
            }
//            private String[] typevehicle, makevehicle, modelvehicle;
            if (vtype.length != 0 && make.length != 0 && model.length != 0) {
                for (int i = 0; i < vtype.length; i++) {
                    if (!vtype[i].equals("") && !make[i].equals("") && !model[i].equals("")) {
//                        System.out.println("(" + i + ") " + vtype[i] + " " + make[i] + " " + model[i]);
                        Vehicle veh = new Vehicle(ci, type[i], make[i], model[i]);
                        listveh.add(veh);
                    }
                }
            }
            if (bank.length != 0 && stotype.length != 0 && accountno.length != 0 && amount.length != 0) {
                for (int i = 0; i < bank.length; i++) {
                    if (!bank[i].equals("") && !type[i].equals("") && !accountno[i].equals("") && !amount[i].equals("")) {
                        try {
                            int acctno = Integer.parseInt(accountno[i]);
                            int amt = Integer.parseInt(amount[i]);
                            Notesreceivable n = new Notesreceivable(ci, bank[i], type[i], acctno, amt);
                            listnotes.add(n);
                        } catch (Exception n) {
                            actionstatus = "Values specified savings/other notes receivable's account number/amount are not valid.";
                            return ERROR;
                        }
                    }
                }
            }

            for (int i = 0; i < reputation.length; i++) {
                Reputation re = rd.getByReputationType(reputation[i]);
                ReputationCreditinv rci = new ReputationCreditinv(re, ci);
                listrci.add(rci);
            }

            for (int i = 0; i < collateral.length; i++) {
                Typeofcollateral t = tcd.getByType(collateral[i]);
                CollateralCreditinv cc = new CollateralCreditinv(t, ci);
//                cc.setDescription("");
                listcci.add(cc);
            }

            for (int i = 0; i < sourceofincome.length; i++) {
                Sourceofincome s = sid.getByType(sourceofincome[i]);
                SourceCreditinv sci = new SourceCreditinv(s, ci);
                listsci.add(sci);
            }

            if (cd.addCreditinvestigation(d, ci, borinfo, listdoc, listinf, listper, listoth, listveh, listnotes, listrci, listcci, listsci, listbusadd)) {
                for (int i = 0; i < listbusupd.size(); i++) {
//                    if (bd.updateBusiness(listbusupd.get(i), listbusid.get(i))) {
//                        actionstatus = "ok";
//                    } else {
//                        actionstatus = "error saving all.";
//                    }
                    try {
                        bd.update(listbusupd.get(i));
                    } catch (Exception ex) {
                        actionstatus = "error saving all.";
                    }
                }

                lv.setWithci("y");
                actionstatus = "Credit Investigation Saved.";
                lvd.update(lv);
            }

            if (userImageFileName != null) {
                if (!userImageFileName.equals("")) {
                    try {

                        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
                        String s = ServletActionContext.getRequest().getRealPath("/");
                        String[] split = s.split("\\\\");
                        ArrayList<String> path = new ArrayList<String>();
                        StringBuffer sb = new StringBuffer();

                        for (int i = 0; i <= split.length - 3; i++) {
                            path.add(i + " " + split[i]);
                            sb.append(split[i]);

                            if (i != split.length - 3) {
                                sb.append("\\");
                            }
                        }

                        sb.append("\\web\\res\\documentimages\\creditinvestigations");
                        String filePath1 = sb.toString();
//                        System.out.println(sb.toString());

//                        System.out.println("1: " + getClass().getClassLoader().getResource(".").getPath());
//                        System.out.println("2: " + ServletActionContext.getRequest().getContextPath());
//                        System.out.println("3: " + ServletActionContext.getRequest().getRealPath("/"));
//                        System.out.println("4: " + servletRequest.getContextPath());
                        File fileToCreate = new File(filePath, this.userImageFileName);
                        File fileToCreate1 = new File(filePath1, this.userImageFileName);

                        FileUtils.copyFile(this.userImage, fileToCreate1);
                        FileUtils.copyFile(this.userImage, fileToCreate);
                    } catch (Exception g) {
                        g.printStackTrace();
                        addActionError(g.getMessage());

                        return INPUT;
                    }
                }
            }


            return SUCCESS;

        } catch (Exception j) {
            j.printStackTrace();
            actionstatus = "There was an error in saving the loan application data. Please try again.";


            return INPUT;

        }
    }
}
